package mqtt

import (
	"errors"
	"fmt"
	"usplatformserver/pkg/errcode"
)

var TopicExportResult = "aitrox/export/result"

func (c *MqttClient) ExportFailed(err *errcode.Error) error {
	if !c.Client.IsConnected() || !c.Client.IsConnectionOpen() {
		return errors.New("MQTT未连接")
	}
	c.ClientPublishMsg(TopicExportResult, fmt.Sprintf(`{"code": %d, "msg": "%s"}`, err.Code(), err.Msg()))

	return nil
}

func (c *MqttClient) ExportSuccess(organType, count int) error {
	if !c.Client.IsConnected() || !c.Client.IsConnectionOpen() {
		return errors.New("MQTT未连接")
	}

	c.ClientPublishMsg(TopicExportResult, fmt.Sprintf(`{"code": 0, "organType": %d, "fileCount": %d, "msg": "导出成功"}`, organType, count))

	return nil
}

func (c *MqttClient) PublishWaitDownloadCount(organType, count int) error {
	if !c.Client.IsConnected() || !c.Client.IsConnectionOpen() {
		return errors.New("MQTT未连接")
	}

	c.ClientPublishMsg(TopicExportResult, fmt.Sprintf(`{"code": 0, "organType": %d, "fileCount": %d}`, organType, count))

	return nil
}

func (c *MqttClient) NotifyMessage(msg string) error {
	if !c.Client.IsConnected() || !c.Client.IsConnectionOpen() {
		return errors.New("MQTT未连接")
	}

	c.ClientPublishMsg(TopicExportResult, msg)

	return nil
}
